/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.fatecpg.songs.entity;

import br.com.fatecpg.songs.entity.util.Searchable;
import br.com.fatecpg.songs.entity.util.Securable;
import java.util.Date;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

/**
 *
 * @author Marcos
 */
@SequenceGenerator(name="SEQSONG", sequenceName="SEQSONG", allocationSize=1, initialValue=1)
@Entity(name="Song")
@Table(name="TB_SONG")
public class Song implements Securable, Searchable{
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQSONG")
    @Column(name = "id")
    private Long id;

    @Column(name = "title")
    private String title;

    @Column(name = "duration")
    private int duration;

    @ManyToOne
    @JoinColumn(name = "id_genre", referencedColumnName = "id")
    private Genre genre;

    @ManyToOne
    @JoinColumn(name = "id_album", referencedColumnName = "id")
    private Album album;

    @Column(name = "lyrics")
    private String lyrics;

    @Column(name = "lyrics_translation")
    private String lyricsTranslation;

    @LazyCollection(LazyCollectionOption.FALSE)
    @ManyToMany
    @JoinTable(name = "TB_SONG_COMPOSER",
            joinColumns = {
                @JoinColumn(name = "id_song", referencedColumnName = "id")},
            inverseJoinColumns = {
                @JoinColumn(name = "id_composer", referencedColumnName = "id")})
    private List<Musician> composers;
    
    @ManyToOne
    @JoinColumn(name = "id_updater", referencedColumnName = "id")
    private User updaterUser;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "last_update")
    private Date lastUpdate;

    public Song() {
        album = new Album();
        genre = new Genre();
    }
    
    @Override
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getDuration() {
        return duration;
    }

    public String getDurationMinutes() {
        int minutes = duration / 60;
        int seconds = duration % 60;
        return minutes + "," + seconds;
    }

    public void setDuration(int duration) {
        this.duration = duration;
    }

    public Genre getGenre() {
        return genre;
    }

    public void setGenre(Genre genre) {
        this.genre = genre;
    }

    public Album getAlbum() {
        return album;
    }

    public void setAlbum(Album album) {
        this.album = album;
    }

    public String getLyrics() {
        return lyrics;
    }

    public void setLyrics(String lyrics) {
        this.lyrics = lyrics;
    }

    public String getLyricsTranslation() {
        return lyricsTranslation;
    }

    public void setLyricsTranslation(String lyricsTranslation) {
        this.lyricsTranslation = lyricsTranslation;
    }

    public List<Musician> getComposers() {
        return composers;
    }

    public void setComposers(List<Musician> composers) {
        this.composers = composers;
    }

    @Override
    public User getUpdaterUser() {
        return updaterUser;
    }

    @Override
    public void setUpdaterUser(User updaterUser) {
        this.updaterUser = updaterUser;
    }

    @Override
    public Date getLastUpdate() {
        return lastUpdate;
    }

    @Override
    public void setLastUpdate(Date lastUpdate) {
        this.lastUpdate = lastUpdate;
    }
}
